//
//  ZCTagsControl.m
//  button固定标签
//
//  Created by apple on 15/11/24.
//  Copyright © 2015年 MDJ. All rights reserved.
//

#import "ZCTagsControl.h"

#define Tmarge 18
#define Theader 35

@implementation ZCTagsControl


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0,0,SCREEN_WIDTH, 175);
        self.titleLable.text = @"热门搜索";
        [self addSubview:self.titleLable];
    }
    return self;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        self.titleLable.text = @"热门搜索";
        [self addSubview:self.titleLable];
    }
    return self;
}

-(void)setTagsArray:(NSArray *)tagsArray{
    _tagsArray = tagsArray;
    [self initSubView:tagsArray];
}

-(void)setBgViewColor:(UIColor *)bgViewColor{
    _bgViewColor = bgViewColor;
    self.backgroundColor = _bgViewColor;
}

-(void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
}


-(void)initSubView:(NSArray *)tags{
    
    int totalloc=3;
    NSLog(@"rectw = %f",self.frame.size.width);
    CGFloat marginOne=Tmarge;
    CGFloat weight=((self.frame.size.width-Tmarge*2 - 20)/totalloc);
    CGFloat height=Theader;
    CGFloat marginTwo=10;
    
    int count = [tags count]%totalloc;
//    self.frame = CGRectMake(0, 0, SCREEN_WIDTH,Theader+32 +(count - 1) * 10 + count *Theader);
    
    for (int i=0; i<[tags count]; i++) {
        int row = i/totalloc; //行
        int loc = i%totalloc; //列
        
        CGFloat appviewx=marginOne+(marginTwo+weight)*loc;
        CGFloat appviewy=Theader+(marginTwo+height)*row;
        
        UIButton *appbtn=[UIButton buttonWithType:UIButtonTypeCustom];
        appbtn.layer.cornerRadius = 4;
        appbtn.tag = i;
        
        NSLog(@"app.x = %f",appviewx);
        appbtn.frame= CGRectMake(appviewx, appviewy, weight, height);
        [appbtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
        appbtn.backgroundColor = [UIColor whiteColor];
        
        if ([tags[i] isKindOfClass:[NSDictionary class]]) {
            NSString *title = [NSString stringWithFormat:@"%@",[tags[i] objectForKey:@"keyword"]];
            [appbtn setTitle:title forState:UIControlStateNormal];
        }
        [appbtn setTitleColor:[UIColor colorWithHexString:@"#8a8a8a"] forState:UIControlStateNormal];
        appbtn.titleLabel.font=[UIFont systemFontOfSize:15.0];
        [self addSubview:appbtn];
        [appbtn setShowsTouchWhenHighlighted:YES];
        [appbtn addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(Tmarge, 0,200, Theader)];
        _titleLable.font = [UIFont systemFontOfSize:15];
        _titleLable.textColor = [UIColor colorWithHexString:@"#b2b2b2"];
        _titleLable.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLable;
}
-(void)ButtonClick:(UIButton *)bender{
    NSLog(@"tag = %ld",(long)bender.tag);
    if (_delegate &&[_delegate respondsToSelector:@selector(ZCTagsControlSelectIndex:andWith:)]) {
        [_delegate ZCTagsControlSelectIndex:bender.tag andWith:bender.titleLabel.text];
    }
}

@end
